package activemq.topic;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * topic send
 * @author yxq
 *
 */
public class TopicPublisher {
	public static void main(String[] args) throws JMSException {
		
		/*ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(
				"yixq", "yxq123456", "tcp://192.168.1.121:61616");*/
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
		"classpath:spring/spring-context.xml");
		
		 ActiveMQConnectionFactory factory =(ActiveMQConnectionFactory) ctx.getBean("targetConnectionFactory");
		Connection connection = factory.createConnection();
		connection.start();
		Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
		Topic topic = session.createTopic("myTopic.messages");
		MessageProducer producer = session.createProducer(topic);
		producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
		for (int i = 0; i < 10; i++) {
			TextMessage message = session.createTextMessage();
			message.setText("message_" + System.currentTimeMillis());
			producer.send(message);
			System.out.println("Sent message: " + message.getText());
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		// session.close();
		// connection.stop();
		// connection.close();
	}
}
